﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Validation in view models</title>
  <keywords>
    <keyword translate="true">ModelAttribute</keyword>
    <keyword translate="true">Models</keyword>
    <keyword translate="true">MVVM</keyword>
    <keyword translate="true">Validation</keyword>
    <keyword translate="true">View models</keyword>
    <keyword translate="true">ViewModelBase</keyword>
    <keyword translate="true">ViewModelToModelAttribute</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Validation in view models</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Validation is very important to provide both feedback to the user, but also to make sure that no invalid data reaches the model or database. Catel offers several ways to implement validation. All options are described in this part of the documentation.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Validation in models via mappings</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The best way is to put validation into a model. Most model objects nowadays implement </text><text styleclass="Normal" style="font-style:italic;" translate="true">INotifyPropertyChanged</text><text styleclass="Normal" translate="true"> and </text><text styleclass="Normal" style="font-style:italic;" translate="true">IDataErrorInfo</text><text styleclass="Normal" translate="true">, the most important classes that are required to use mappings from/to a model inside a view model.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The great advantage of mapping properties from/to models automatically using Catel is that you don&apos;t have to write lots of plumbing yourself (getting and setting values in the model and view model). However, if the model implements </text><text styleclass="Normal" style="font-style:italic;" translate="true">INotifyPropertyChanged</text><text styleclass="Normal" style="font-style:normal;" translate="true"> and </text><text styleclass="Normal" style="font-style:italic;" translate="true">IDataErrorInfo</text><text styleclass="Normal" style="font-style:normal;" translate="true">, Catel also automatically uses the validation from the model. For example, if there is a </text><text styleclass="Normal" style="font-style:italic;" translate="true">Person</text><text styleclass="Normal" style="font-style:normal;" translate="true"> model that checks if the </text><text styleclass="Normal" style="font-style:italic;" translate="true">FirstName</text><text styleclass="Normal" style="font-style:normal;" translate="true"> and </text><text styleclass="Normal" style="font-style:italic;" translate="true">LastName </text><text styleclass="Normal" style="font-style:normal;" translate="true">are entered, why rewrite this validation again in the view model?</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">There are two ways to use automatic mappings.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Mapping via ViewModelToModelAttribute</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Mapping a model property by using the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelToModelAttribute" styleclass="Normal" translate="true">ViewModelToModelAttribute</link><text styleclass="Normal" translate="true"> requires the definition of a model property and a separate property per mapped property. The code below automatically maps the </text><text styleclass="Normal" style="font-style:italic;" translate="true">FirstName</text><text styleclass="Normal" translate="true"> property.</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Gets or sets the person.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[Model]</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> Person Person</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;get { </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">return</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> GetValue&lt;Person&gt;(PersonProperty); }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> set { SetValue(PersonProperty, value); }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Register the Person property so it is known in the class.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">static</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">readonly</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> PropertyData PersonProperty = RegisterProperty(&quot;Person&quot;, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(Person));</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Gets or sets the first name.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[ViewModelToModel(&quot;Person&quot;)]</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> FirstName</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;get { </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">return</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> GetValue&lt;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&gt;(FirstNameProperty); }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;set { SetValue(FirstNameProperty, value); }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Register the FirstName property so it is known in the class.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">static</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">readonly</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> PropertyData FirstNameProperty = RegisterProperty(&quot;FirstName&quot;, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">));</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If the </text><text styleclass="Normal" style="font-style:italic;" translate="true">Person.FirstName</text><text styleclass="Normal" style="font-style:normal;" translate="true"> provides an error via the </text><text styleclass="Normal" style="font-style:italic;" translate="true">IDataErrorInfo</text><text styleclass="Normal" style="font-style:normal;" translate="true"> interface, it will automatically be exposed by the view model as well.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Mapping via ExposeAttribute</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Mapping a property via the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ExposeAttribute" styleclass="Normal" style="font-style:normal;" translate="true">ExposeAttribute</link><text styleclass="Normal" style="font-style:normal;" translate="true"> is even more simple, but has some disadvantages. First, let&apos;s talk about the disadvantages before showing the code:</text></para>
    <list id="44" type="ol" listtype="decimal" formatstring="&#37;&#48;&#58;&#115;&#46;" format-charset="DEFAULT_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal" style="font-family:Arial; font-size:10pt; color:#000000;">
      <li styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">Catel uses the </text><text styleclass="Normal" style="font-style:italic;" translate="true">ICustomTypeDescriptor</text><text styleclass="Normal" style="font-style:normal;" translate="true"> interface to implement this behavior, and the interface is only available in WPF.</text></li>
      <li styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">The properties are not actually created, so you cannot use them inside your view model as real properties.</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">In other words, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ExposeAttribute" styleclass="Normal" style="font-style:normal;" translate="true">ExposeAttribute</link><text styleclass="Normal" style="font-style:normal;" translate="true"> is only very useful if 1) you are using WPF and 2) if the property is not used inside the view model but only declared to protect the model from the outside world. If both of these constraints are true, then the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ExposeAttribute" styleclass="Normal" style="font-style:normal;" translate="true">ExposeAttribute</link><text styleclass="Normal" style="font-style:normal;" translate="true"> is definitely worth taking a look at. The usage is very simple:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Gets or sets the person.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[Model]</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[Expose(&quot;FirstName&quot;)]</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[Expose(&quot;MiddleName&quot;)]</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[Expose(&quot;LastName&quot;)]</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">private</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160;Person Person</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;get { </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">return</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> GetValue&lt;Person&gt;(PersonProperty); }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;set { SetValue(PersonProperty, value); }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Register the Person property so it is known in the class.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">static</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">readonly</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> PropertyData PersonProperty = RegisterProperty(&quot;Person&quot;, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">typeof</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(Person));</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">Simple declare the model property and decorate it with one or multiple </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ExposeAttribute" styleclass="Normal" style="font-style:normal;" translate="true">ExposeAttribute</link><text styleclass="Normal" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> instances. Not only are the properties automatically available for binding, the view model also checks for errors and automatically maps these as well.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Validation in view models</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Until now, we only spoke about automatic validation for validation that was written in the model. However, sometimes it is required to write validation inside the view model as well. One reason might be that the model is a POCO object not providing any validation. Or, sometimes there is a logical error that has nothing to do with the model, but should be shown to the user anyway.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">In such a case, Catel offers lots of possibilities to write custom validation inside the view model. Below are the possibilities:</text></para>
    <list id="45" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal (list)" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Field warnings</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Field errors</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Business rule warnings</text></li>
      <li styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Business rule errors</text></li>
    </list>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The difference between a field and business rule is that a field error or warning is specific for a property. These are returned via </text><text styleclass="Normal" style="font-style:italic;" translate="true">IDataErrorInfo[&quot;propertyName&quot;]</text><text styleclass="Normal" style="font-style:normal;" translate="true">. A business rule is a rule that applies to multiple fields or even a whole entity. Business rule validations are returned via </text><text styleclass="Normal" style="font-style:italic;" translate="true">IDataErrorInfo.Error</text><text styleclass="Normal" style="font-style:normal;" translate="true">. </text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">To implement validation into a view model, only two methods need to be implemented. Catel clearly separates the field validation from the business rule validation to make it much clearer to the developer what is going on.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Validating fields</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To validate fields, one should override the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_Data_DataObjectBase_ValidateFields" styleclass="Normal" style="font-style:normal;" translate="true">ValidateFields</link><text styleclass="Normal" style="font-style:normal;" translate="true"> method. Below is an example of field validation on a view model:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Validates the field values of this object. Override this method to enable</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// validation of field values.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;param name=&quot;validationResults&quot;&gt;The validation results, add additional results to this list.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">protected</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">override</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> ValidateFields(List&lt;FieldValidationResult&gt; validationResults)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> (!</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">.IsNullOrEmpty(FirstName))</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;validationResults.Add(FieldValidationResult.CreateError(FirstNameProperty, &quot;First name cannot be empty&quot;));</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Validating business rules</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To validate business rules, one should override the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_Data_DataObjectBase_ValidateBusinessRules" styleclass="Normal" style="font-style:normal;" translate="true">ValidateBusinessRules</link><text styleclass="Normal" style="font-style:normal;" translate="true"> method. Below is an example of business rule validation on a view model:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Validates the field values of this object. Override this method to enable</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// validation of field values.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;param name=&quot;validationResults&quot;&gt;The validation results, add additional results to this list.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">protected</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">override</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> ValidateBusinessRules(List&lt;BusinessRuleValidationResult&gt; validationResults)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> (SomeBusinessErrorOccurs)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;validationResults.Add(BusinessRuleValidationResult.CreateError(&quot;A business error occurred&quot;));</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Translating model validation in the view model</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Thanks to the validation system in Catel, it is very easy to implement very advanced validation features in view models. The example below shows how to translate errors that are added to a model in the Data Access Layer or validation layer. Assume that the following pseudo code is used to set an error on a model in the DAL:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">SetFieldError(“FirstName”,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">“FirstNameRequired”);</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">All errors that are mapped from the model to the view model automatically are available in the </text><text styleclass="Normal" style="font-style:italic;" translate="true">validationResults</text><text styleclass="Normal" style="font-style:normal;" translate="true"> parameter. This way, the error can be easily translated:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// Validates the field values of this object. Override this method to enable </text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// validation of field values.</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;/summary&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:italic; color:#800000;" translate="true">/// &lt;param name=&quot;validationResults&quot;&gt;The validation results, add additional results to this list.&lt;/param&gt;</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">protected</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">override</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> ValidateFields(List&lt;FieldValidationResult&gt; validationResults)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">foreach</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> (var validationResult </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">in</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> validationResults)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; {</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> (validationResult.Message == &quot;FirstNameRequired&quot;)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160;{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160; validationResult.Message = Properties.Resources.FirstNameRequired; </text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160;}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; }</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Of course this is not something you want to actually do in your view model, so you’ll probably have to write a helper class that translates the validation for you. You might or might not like delaying the translation of the model errors to as close as the view, but it shows how extremely powerful the improved validation of Catel is. And if you think a bit about it, wouldn’t it be a good idea to delay the translation from the server to the actual client to as close as the view…?</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Validating via annotations</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Some people like to add validation to their (view)models using annotations (attributes). Catel also supports this method, but adds additional functionality. The idea behind it is that in the end, Catel always provides all errors of an object via the </text><text styleclass="Normal" style="font-style:italic;" translate="true">IDataErrorInfo</text><text styleclass="Normal" translate="true"> interface. This means that when attribute validation is used, the errors are internally registered and provided in the </text><text styleclass="Normal" style="font-style:italic;" translate="true">ValidateFields</text><text styleclass="Normal" translate="true"> method. This way, all types of validation that are provided by the .NET framework are gathered into one single location where they can be used by the view.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">To validate required fields or not to validate required fields at startup</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel does not validate the properties with data annotations at startup. It will only validate the data annotations when properties change or when the view model is about to be saved. This is implemented this way to allow a developer to show required fields with an asterisk (*) instead of errors. If a developer still wants to initially display errors, only a single call has to be made in the constructor:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">Validate(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">true</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">, </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000080;" translate="true">false</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">);</text></para>
  </body>
</topic>
